Shader "BToolkit/Outline"
{
	Properties
	{
		_MainTex("Main Texture", 2D) = "white" {}
		_Color("Tint", Color) = (1, 1, 1, 1)

		_StencilComp("Stencil Comparison", Float) = 8
		_Stencil("Stencil ID", Float) = 0
		_StencilOp("Stencil Operation", Float) = 0
		_StencilWriteMask("Stencil Write Mask", Float) = 255
		_StencilReadMask("Stencil Read Mask", Float) = 255

		_ColorMask("Color Mask", Float) = 15

		[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
	}
		SubShader
		{
			Tags
			{
				"Queue" = "Transparent"
				"IgnoreProjector" = "True"
				"RenderType" = "Transparent"
				"PreviewType" = "Plane"
				"CanUseSpriteAtlas" = "True"
			}

			Stencil
			{
				Ref[_Stencil]
				Comp[_StencilComp]
				Pass[_StencilOp]
				ReadMask[_StencilReadMask]
				WriteMask[_StencilWriteMask]
			}

			Cull Off
			Lighting Off
			ZWrite Off
			ZTest[unity_GUIZTestMode]
			Blend SrcAlpha OneMinusSrcAlpha
			ColorMask[_ColorMask]

			Pass
			{
				Name "OUTLINE"

				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag

				sampler2D _MainTex;
				fixed4 _Color;
				fixed4 _TextureSampleAdd;
				float4 _MainTex_TexelSize;

				struct appdata
				{
					float4 vertex : POSITION;
					float2 uv0 : TEXCOORD0;
					float2 uv1 : TEXCOORD1;
					float4 uv2 : TEXCOORD2;
					float4 uv3 : TEXCOORD3;
					fixed4 color : COLOR;
				};

				struct v2f
				{
					float4 vertex : SV_POSITION;
					float2 uv0 : TEXCOORD0;
					float2 uv1 : TEXCOORD1;
					float4 uv2 : TEXCOORD2;
					float4 uv3 : TEXCOORD3;
					fixed4 color : COLOR;
				};

				v2f vert(appdata IN)
				{
					v2f o;
					o.vertex = UnityObjectToClipPos(IN.vertex);
					o.uv0 = IN.uv0;
					o.uv1 = IN.uv1;
					o.uv2 = IN.uv2;
					o.uv3 = IN.uv3;
					o.color = IN.color * _Color;
					return o;
				}

				fixed IsInRect(float2 pPos, float2 pClipRectXY, float2 pClipRectZW)
				{
					pPos = step(pClipRectXY, pPos) * step(pPos, pClipRectZW);
					return pPos.x * pPos.y;
				}

				fixed SingleVertex(int pIndex, v2f IN, float outlineSize)
				{
					const fixed sinArray[12] = { 0, 0.5, 0.866, 1, 0.866, 0.5, 0, -0.5, -0.866, -1, -0.866, -0.5 };
					const fixed cosArray[12] = { 1, 0.866, 0.5, 0, -0.5, -0.866, -1, -0.866, -0.5, 0, 0.5, 0.866 };
					float2 pos = IN.uv0 + _MainTex_TexelSize.xy * float2(cosArray[pIndex], sinArray[pIndex]) * outlineSize;
					return IsInRect(pos, IN.uv1, IN.uv2) * (tex2D(_MainTex, pos) + _TextureSampleAdd).w;
				}

				fixed4 frag(v2f IN) : SV_Target
				{
					fixed4 color = (tex2D(_MainTex, IN.uv0) + _TextureSampleAdd) * IN.color;
				//去除重复贴图露出的头
				color.w *= IsInRect(IN.uv0, IN.uv1, IN.uv2);
				half4 val = half4(IN.uv3.x, IN.uv3.y, IN.uv3.z, 0);

				if (IN.uv3.w > 0)
				{
					//描边
					float outlineSize = IN.uv3.w;
					val.w += SingleVertex(0, IN, outlineSize);
					val.w += SingleVertex(1, IN, outlineSize);
					val.w += SingleVertex(2, IN, outlineSize);
					val.w += SingleVertex(3, IN, outlineSize);
					val.w += SingleVertex(4, IN, outlineSize);
					val.w += SingleVertex(5, IN, outlineSize);
					val.w += SingleVertex(6, IN, outlineSize);
					val.w += SingleVertex(7, IN, outlineSize);
					val.w += SingleVertex(8, IN, outlineSize);
					val.w += SingleVertex(9, IN, outlineSize);
					val.w += SingleVertex(10, IN, outlineSize);
					val.w += SingleVertex(11, IN, outlineSize);
				}
				else
				{
					//阴影
					float shadowSize = IN.uv2.w;
					val.w += SingleVertex(0, IN, shadowSize);
					val.w += SingleVertex(1, IN, shadowSize);
					val.w += SingleVertex(2, IN, shadowSize);
					val.w += SingleVertex(3, IN, shadowSize);
					val.w += SingleVertex(4, IN, shadowSize);
					val.w += SingleVertex(5, IN, shadowSize);
					val.w += SingleVertex(6, IN, shadowSize);
					val.w += SingleVertex(7, IN, shadowSize);
					val.w += SingleVertex(8, IN, shadowSize);
					val.w += SingleVertex(9, IN, shadowSize);
					val.w += SingleVertex(10, IN, shadowSize);
					val.w += SingleVertex(11, IN, shadowSize);
				}

				val.w = clamp(val.w, 0, 1);
				color = (val * (1.0 - color.a)) + (color * color.a);
				color.a *= IN.color.a;

				return color;
			}
			ENDCG
		}
		}
}
